<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <link rel="stylesheet" href="../../../js/layui/css/layui.css" media="all">
    <script src="../../../js/layui/layui.js"></script>
    <script src="../../../js/myJQ.js"></script>

    <style type="text/css">
        .layui-upload-list{
            vertical-align: middle;
        }
    </style>
</head>
<body>
<fieldset class="layui-elem-fisseld layui-field-title" style="margin-top: 5px;">
    <legend>商品信息</legend>
</fieldset>
    <div style="padding: 5px; background-color: #F2F2F2;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md5">
                <div class="layui-card">
                    <div class="layui-card-header">商品图片</div>
                    <div class="layui-card-body">
                        <div class="layui-upload">
                            <div class="layui-upload-list" style="width: 28rem;height: auto;">
                                <img class="layui-upload-img" id="img" style="width: 28rem;height: auto;" onError="this.src='../../../images/defaulPic.png';">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md7">
                <div class="layui-card">
                    <div class="layui-card-header">商品信息</div>
                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品名:</label>
                            <div class="layui-input-block" >
                                <input type="text" disabled id="productName" lay-verify="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品说明:</label>
                            <div class="layui-input-block" >
                                <input type="text" disabled id="description" lay-verify="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">商品价格:</label>
                            <div class="layui-input-block" >
                                <input type="text" disabled id="price" lay-verify="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">购买份数:</label>
                            <div class="layui-input-block" >
                                <button type="button" data-type="sub" style="width: 3rem;float: left; margin-top: 5px;" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon"></i></button>
                                <input type="text" style="width: 2.5rem;float: left;border: solid 3px #FFFFFF" disabled id="purchaseNum" lay-verify="title" autocomplete="off" class="layui-input">
                                <button type="button" data-type="plus" style="width: 3rem;float: left;margin-top: 5px;" class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon"></i></button>
                                <label class="layui-form-label">总金额:</label>
                                <input type="text" style="float: left;display: inline;width: 20%" disabled id="amount" lay-verify="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                       <!-- <div class="layui-form-item">
                            <label class="layui-form-label">总金额:</label>
                            <div class="layui-input-block" >
                                <input type="text" disabled id="amount" lay-verify="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>-->
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">联系电话:</label>
                            <div class="layui-input-block">
                                <input type="text" id="phone" lay-verify="title" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">收件地址:</label>
                            <div class="layui-input-block">
                                <textarea placeholder="请输入内容" id="address" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" id="submit" class="layui-btn" data-type="submit">提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary" id="back">返回</button>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(["jquery","layer"],function () {
            var $ = layui.$;
            var layer = layui.layer;
            var basePath = "",param = "";

            $(function () {
                basePath = getRootPath();
                param = RequestParameter();
                getProductDetail(param);
            })
            //获取商品详情
            function getProductDetail(data) {
                debugger
                var params = {};
                params.productId = data.productId;
                $.ajax({
                    url:basePath+"/product/getProductList",
                    type:"get",
                    dataType:'json',
                    data:params,
                    cache:false,
                    beforeSend :function(xmlHttp){
                        xmlHttp.setRequestHeader("If-Modified-Since","0");  //添加
                        xmlHttp.setRequestHeader("Cache-Control","no-cache"); //添加
                    },
                    success:function (result) {
                        var obj = result.data[0];
                        $("#productName").val(obj.productName);
                        $("#price").val(obj.price);
                        $("#num").val(obj.num);
                        $("#purchaseNum").val(1);
                        $("#amount").val(obj.price+" 元");
                        $("#amount").attr("name",obj.price);
                        $("#submit").val(obj.productId);
                        $("#description").val(obj.description);
                        var img = obj.img;
                        if(typeof(img) !="undefined"){
                            $('#img').attr('src', "../"+obj.img)
                        }
                    }
                })
            }

            var active ={
                sub:function () {
                    debugger;
                    var purchaseNum = parseInt($("#purchaseNum").val());
                    var amount = 0;
                    if(purchaseNum<=0){
                        $("#purchaseNum").val(0);
                        $("#amount").val(0+" 元");
                    }else {
                        purchaseNum = purchaseNum-1;
                        $("#purchaseNum").val(purchaseNum);
                        var amount = parseInt($("#price").val())*purchaseNum;
                        $("#amount").val(amount+" 元");
                    }
                    $("#amount").attr("name",amount);
                },
                plus:function () {
                    debugger;
                    var purchaseNum = parseInt($("#purchaseNum").val());
                    //单价
                    var price = parseInt($("#price").val());
                    var amount = 0;
                    if(purchaseNum<=0){
                        $("#purchaseNum").val(1);
                        $("#amount").val(price*1+" 元");
                    }else {
                        purchaseNum = purchaseNum+1;
                        $("#purchaseNum").val(purchaseNum);
                        amount = parseInt($("#price").val())*purchaseNum;
                        $("#amount").attr("name",amount);
                        $("#amount").val(amount+" 元");
                    }
                    $("#amount").attr("name",amount);
                },
                submit:function () {
                    debugger;
                    var prodcutId = $("#submit").val();
                    var address = $("#address").val();
                    if(typeof(address)=="undefined" || address == null || address.trim().length<=0){
                        parent.layer.msg('地址不能为空！',{
                            icom:1,
                            time:2000
                        });
                        return;
                    }
                    var purchaseNum = parseInt($("#purchaseNum").val());
                    if(purchaseNum<=0){
                        parent.layer.msg('购买份数不能为0！',{
                            icom:1,
                            time:2000
                        });
                        return;
                    }
                    var amount = $("#amount").attr("name");
                    var phone = $("#phone").val();
                    /*if (!isPhoneNum(phone)){
                        parent.layer.msg('请输入正确的联系电话(座机:区号-号码、或11位手机号)！',{
                            icom:1,
                            time:2000
                        });
                        return;
                    }*/
                    parent.layer.confirm('确认提交？', {
                        btn: ['确认','取消'] //按钮
                    }, function(){
                        var index = layer.load();
                        var param = {};
                        param.productId=prodcutId;
                        param.address=address;
                        param.num = purchaseNum;
                        param.amount = amount;
                        param.phone = phone;
                        $.ajax({
                            url:basePath+'/order/addOrder',
                            dataType:'json',
                            data:param,
                            cache:false,
                            beforeSend :function(xmlHttp){
                                xmlHttp.setRequestHeader("If-Modified-Since","0");  //添加
                                xmlHttp.setRequestHeader("Cache-Control","no-cache"); //添加
                            },
                            success:function(result){
                                console.log(JSON.stringify(result));
                                parent.layer.msg(result.msg, {
                                    icon: 1,
                                    time:2000
                                });
                            },
                            async:false
                        });
                        layer.close(index);
                    }, function(){
                        debugger
                        parent.layer.msg('取消成功！', {
                            icon: 1,
                            time: 2000
                        });
                    });
                },
                reset:function () {
                    parent.layer.confirm('确认重置？', {
                        btn: ['确认','取消']
                    }, function(){
                        parent.layer.msg('重置成功', {
                            icon: 1,
                            time:2000
                        });
                    }, function(){
                        parent.layer.msg('取消成功！', {
                            icon: 1,
                            time: 2000
                        });
                    });
                }
            }

            $('.layui-btn').on('click',function () {
                debugger
                var type = $(this).data('type');
                active[type]?active[type].call(this):'';
            });


            $("#back").click(function () {
                window.location.href = basePath+"/order/index";
            });

            /**
             * 如果是编辑，获取该数据的ID
             * @returns {Object}
             * @constructor
             */
            function RequestParameter(){
                debugger;
                var url = window.location.search; //获取url中"?"符后的字串
                var theRequest = new Object();
                if (url.indexOf("?") != -1) {
                    var str = url.substr(1);
                    var strs = str.split("&");
                    for(var i = 0; i < strs.length; i ++) {
                        theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
                    }
                }
                return theRequest;
            }

            /**
             * 电话号码校验
             */
            function isPhoneNum(phone){
                var testPhone=/^(1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8})?$/;
                var testFixedPhone=/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
                if(!testPhone.test(phone) && !testFixedPhone.test(phone)){
                   return false;
                }else {
                    return true;
                }
            }
        });

    </script>

</body>
</html>